package com.ruoyi.common.utils;

import cn.hutool.core.date.DatePattern;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class DateUtil {
    /**
     * 判断某日期是否超过指定天数
     * @param beforeDate 待判断日期
     * @param days 天数
     * @return
     */
    public static boolean beforeNowDays(Date beforeDate,Long days){
        LocalDate now = LocalDate.now();
        LocalDate localDate = beforeDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        long daysBetween = ChronoUnit.DAYS.between(now, localDate);
        if (daysBetween > 3) {
            return true;
        }
        return false;
    }

    /**
     * 将指定格式的日期字符串转换为Date对象
     *
     * @param dateString 日期字符串
     * @param pattern    日期字符串的格式
     * @return Date对象，转换失败时返回null
     */
    public static Date stringToDate(String dateString, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        try {
            return dateFormat.parse(dateString);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 将Date对象转换为指定格式的日期字符串
     *
     * @param date    Date对象
     * @param pattern 日期字符串的格式，如"yyyy-MM-dd"
     * @return 指定格式的日期字符串，转换失败时返回空字符串
     */
    public static String dateToString(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    /**
     * 将日期字符串转换为Date对象(yyyy-MM-dd HH:mm:ss)
     *
     * @param dateString
     * @return
     */
    public static Date stringToDate(String dateString) {
        return stringToDate(dateString, DatePattern.NORM_DATETIME_PATTERN);
    }

    /**
     * 将Date对象转换为日期字符串(yyyy-MM-dd)
     *
     * @param date
     * @return
     */
    public static String dateToString(Date date) {
        return dateToString(date, DatePattern.NORM_DATE_PATTERN);
    }

    /**
     * 获取当前日期(yyyy-MM-dd)
     *
     * @return
     */
    public static String getCurrentDate() {
        LocalDate currentDate = LocalDate.now();
        return currentDate.format(DatePattern.NORM_DATE_FORMATTER);
    }

    public static String getPreviousMonth(String inputDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        LocalDate date = LocalDate.parse(inputDate + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate lastMonth = date.minusMonths(1);
        return lastMonth.format(formatter);
    }



    public static String getPreviousMonthByTimeFormat(String inputDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN);
        LocalDateTime date = LocalDateTime.parse(inputDate, DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));
        LocalDateTime lastMonth = date.minusMonths(1);
        return lastMonth.format(formatter);
    }

    /**
     * 判断某个日期相隔现在多少分钟
     * @param date
     * @return
     */
    public static Long diffMinute(Date date){
        Long time = new Date().getTime()-date.getTime();
        return time/1000/60;
    }

    public static String packStartTime(String startTime){
        if(StringUtils.isNotBlank(startTime)){
            return startTime + " 00:00:00";
        }
        return startTime;
    }

    public static String packEndTime(String endTime){
        if(StringUtils.isNotBlank(endTime)){
            return endTime + " 23:59:59";
        }
        return endTime;
    }

}
